package com.neu.cpsp.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neu.cpsp.pojo.TeamGroup;
import com.neu.cpsp.pojo.TeamGroupInfo;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TeamGroupMapper extends BaseMapper<TeamGroup>
{
    //如果班组表中分管和主管的idCard在employee表没有对应的数据，当前使用的sql语句无法查出该条数据
    //如果需修改，可以使用下面注释的sql语句进行查询，如果在employee中没有对应的数据，分管和主管的姓名将赋予null
    //SELECT t.*, e1.`name` branchManagerName, e2.`name` mainManagerId FROM team_group t
    //left outer join employee e1 on t.branch_manager_id = e1.idCard
    //left outer join employee e2 on t.main_manager_id = e2.idCard
    @Select({"<script>",
            "SELECT t.*, e1.`name` branchManagerName, e2.`name` mainManagerName ",
            "FROM `team_group` t, employee e1, employee e2 ",
            "WHERE t.branch_manager_id = e1.idCard and t.main_manager_id = e2.idCard ",
            " <if test=\"departmentId != null and departmentId != '' \" >",
            " and t.department_id = #{departmentId}",
            " </if>",
            "</script>"})
    List<TeamGroupInfo> findTeamGroupAndName(String departmentId);

    @Select({"<script>",
            "select *",
            "from `team_group`",
            "where id = #{id}",
    "</script>"})
    TeamGroup selectById(Integer id);

    @Select({"<script>",
            "select * ",
            "from `team_group`",
            "where department_id = #{departmentId}",
            "</script>"})
    List<TeamGroup> findTeamGroupByDepart(String departmentId);
}
